// pages/userConsole/userConsole.js
import Notify from '../../dist/notify/notify';
const db = wx.cloud.database()
Page({

  data: {
    shows: true,
    minDate: new Date().getTime()-864000000,
    defaultdate:new Date().getTime(),
    maxDate:new Date().getTime(),
    loading:false,
    actions: [
      {
        name: '支出',
      },
      {
        name: '收入',
      },
    ],
    typedata: [
      {
        name: '餐饮',
        icon: '../../icon/food.png',
        active: false
      },
      {
        name: '交通',
        icon: '../../icon/jiaotong.png',
        active: false
      },
      {
        name: '住房',
        icon: '../../icon/housing.png',
        active: false
      },
      {
        name: '美容',
        icon: '../../icon/meirong.png',
        active: false
      },
      {
        name: '服装',
        icon: '../../icon/fushilei.png',
        active: false
      },
      {
        name: '旅游',
        icon: '../../icon/lvyou.png',
        active: false
      },
      {
        name: '娱乐',
        icon: '../../icon/yule.png',
        active: false
      },
      {
        name: '医疗',
        icon: '../../icon/yiliao.png',
        active: false
      },
      {
        name: '通讯',
        icon: '../../icon/tongxun.png',
        active: false
      },
      {
        name: '学习',
        icon: '../../icon/xuexi.png',
        active: false
      },
      {
        name: '数码',
        icon: '../../icon/shumapeijian.png',
        active: false
      },
      {
        name: '其他',
        icon: '../../icon/qita.png',
        active: false
      }
    ],
    typedatas: [
      {
        name: '工资',
        icon: '../../icon/gongzi.png',
        active: false
      },
      {
        name: '兼职',
        icon: '../../icon/jianzhi.png',
        active: false
      },
      {
        name: '礼金',
        icon: '../../icon/lijin.png',
        active: false
      },
      {
        name: '奖金',
        icon: '../../icon/jiangjin.png',
        active: false
      },
      {
        name: '其他',
        icon: '../../icon/qita.png',
        active: false
      }
    ],
    show: false,
    date: '',
    message: '',
    money: '',
    expenditure: true,
    _openid: "",
    type: 0,
    recordtype: '',
    icondata:'',
    steppervalue:1
  },
  onLoad: function () {
    let that = this
    wx.cloud.callFunction({
      name: "login",
      complete: res => {
        this.setData({ _openid: res.result.openid });
      }
    })
  },
  onShow: function (options) {
    this.setData({ shows: true,date:'',message:'',steppervalue:1 });
  },
  onSelect(event) {

    if (event.detail.name) {

      if (event.detail.name == '支出') {
        this.setData({ shows: false, expenditure: true, type: 0 });
      } else {
        this.setData({ shows: false, expenditure: false, type: 1 });
      }
    } else {
      return
    }
  },
  ceshi(event) {
    let that = this
    console.log(event.currentTarget.dataset.message)
    that.setData({ recordtype: event.currentTarget.dataset.message,icondata: event.currentTarget.dataset.icon});
    let typelist = that.data.type == '0'?that.data.typedata:that.data.typedatas
    for (var i = 0; i < typelist.length; i++) {
      typelist[i].active = false;
      that.data.type == '0'?that.setData({ typedata: typelist }):that.setData({ typedatas: typelist })
      if (typelist[i].name == event.currentTarget.dataset.message) {
        typelist[i].active = true;
        that.data.type == '0'?that.setData({ typedata: typelist }):that.setData({ typedatas: typelist })
      }
    }
  },
  calendar() {
    this.setData({
      show: true
    });
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    const dt = new Date(date);
    const y = dt.getFullYear();
    const m = (dt.getMonth() + 1 + "").padStart(2, "0");
    const d = (dt.getDate() + "").padStart(2, "0");
    return `${y}-${m}-${d}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  onChange(event) {
    this.setData({ money: event.detail });
  },
  magdata(e){
    this.setData({ message: e.detail });
  },
  preservation() {
    if (!this.data.recordtype) {
      Notify({ type: 'warning', message: '请选择支出类型哦！' });
    } else if (!this.data.date) {
      Notify({ type: 'warning', message: '请选择支出日期哦！' });
    } else if (!this.data.money) {
      Notify({ type: 'warning', message: '请输入金额哦！' });
    }else {
      this.setData({ loading: true});
      let postdata = {
        type: this.data.type,
        recordtype: this.data.recordtype,
        date: this.data.date,
        remarks: this.data.message,
        money: this.data.type == '0'?this.data.money*-1: Number(this.data.money),
        userid: wx.getStorageSync('id'),
        icon:this.data.icondata
      }
      console.log(postdata)
      db.collection('record').add({
        data: postdata
      }).then(res => {
        this.setData({ stepper: 0,loading:false,shows:true,date:'',message:'',steppervalue:1});
        Notify({ type: 'success', message: '增加成功' });
      })
    }

  }

})